package com.example.springboot.service;

import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserResponsitory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

import static java.util.Collections.emptyList;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private UserResponsitory userResponsitory;

    public UserDetailsServiceImpl(UserResponsitory userResponsitory) {
        this.userResponsitory = userResponsitory;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userResponsitory.findByUsername(username);
        if(user == null){
            throw new UsernameNotFoundException(username);
        }
        org.springframework.security.core.userdetails.User user1 = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Collections.<GrantedAuthority>emptyList());
        return user1;
    }
}
